<template>
	<view class="container">
		<view class="item">
			<u-swiper
				:list="info.imagesUrl"
				mode="round" 
				indicator-pos="bottomRight" 
				height="380"
				border-radius="0"
			></u-swiper>
			<!-- <image src="../../static/p3.jpg" mode=""></image> -->
			<view class="item_txt">
				<text>{{ info.title }}</text>
				<text class="des">{{ info.description }}</text>
			</view>
		</view>
		<view class="news">
			<view class="news_header">
				<text>服务详情</text>
			</view>
			<view class="news_body">
				<rich-text :nodes="info.content"></rich-text>
			</view>
		</view>
		<view class="foot-btns">
			<view class="btn" @click="copyWexin">
				<u-icon name="weixin-fill" color="#4caf50" size="46"></u-icon>
				<text>添加微信</text>
			</view>
			<view class="btn" v-if="isBindPhone" @click="consulting">
				<u-icon name="chat-fill" color="#4caf50" size="46"></u-icon>
				<text>在线咨询</text>
			</view>
			<button v-else open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
				<u-icon name="chat-fill" color="#4caf50" size="46"></u-icon>
				<text>在线咨询</text>
			</button>
		</view>
	</view>
</template>

<script>
	import { medicineDetail, contactUs, bindPhone } from '../../api/api'
	export default {
		data() {
			return {
				info: {},
				weixin: '',
				isBindPhone: false,
				code: ''
			};
		},
		onLoad(options) {
			this.getDetail(options.id)
			this.getWeixin()
		},
		onShow() {
			const mobile = uni.getStorageSync('userInfo').mobile
			if(mobile) {
				this.isBindPhone = true
			} else {
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						// 获取用户信息
						this.code = loginRes.code
					}
				});
				// #endif
			}
		},
		methods: {
			getDetail(id) {
				medicineDetail({ id }).then(res => {
					if(res.code == 1) {
						this.info = res.data
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			// 获取店铺微信号
			getWeixin() {
				contactUs().then(res => {
					if(res.code == 1) {
						this.weixin = res.data.wechat
					} else {
						this.$u.toast(res.msg)
					}
				})
			},
			copyWexin() {
				uni.setClipboardData({
				    data: this.weixin,
				    success: function () {
				        console.log('success');
				    }
				});
			},
			consulting() {
				uni.navigateTo({
					url: '/pages/form/form?id=' + this.info.id + '&module=zytl'
				})
			},
			onGetPhoneNumber(e) {
				if(e.detail.errMsg == "getPhoneNumber:ok"){
					const { iv, encryptedData } = e.detail
					bindPhone({
						token: uni.getStorageSync('userInfo').token,
						code: this.code,
						encryptedData,
						iv
					}).then(res => {
						if (res.code == 1) {
							uni.setStorageSync('userInfo', res.data)
							uni.navigateTo({
								url: '/pages/form/form?id=' + this.info.id + '&module=zytl'
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				}else{
					uni.showToast({
						title: '取消授权',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #e8e8e8;
	}
	.container {
		padding-bottom: 50rpx;
	}
	.item {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 8rpx;
		overflow: hidden;
		margin-bottom: 25rpx;

		image {
			width: 100%;
			height: 380rpx;
		}

		.item_txt {
			display: flex;
			flex-direction: column;
			padding: 20rpx;

			text {
				display: block;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 32rpx;
			}

			.des {
				color: #888;
				font-size: 28rpx;
				margin-top: 10rpx;
			}
		}
	}
	.news {
		width: 100%;
		background-color: #fff;
		margin-top: 15rpx;
		
		.news_header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			padding: 30rpx 25rpx;
			border-bottom: 1px solid #eee;
			text {
				position: relative;
				font-size: 32rpx;
				font-weight: 700;
				padding-left: 30rpx;
				&::before {
					content: '';
					position: absolute;
					top: 0;
					left: 0;
					width: 8rpx;
					height: 100%;
					border-radius: 4rpx;
					background-color: #4CD964;
				}
			}
		}
		.news_body {
			width: 100%;
			padding: 25rpx;
			text-align: justify;
		}
	}
	.foot-btns {
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #fff;
		width: 100%;
		height: 100rpx;
		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 50%;
			height: 100%;
			&:first-child {
				border-right: 1px solid #eee;
			}
			text {
				font-size: 30rpx;
				margin-left: 10rpx;
			}
		}
		/deep/ button {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 50%;
			font-size: 30rpx;
			background-color: #fff;
			text {
				margin-left: 10rpx;
			}
		}
		button:after {
			border: 0;
		}
	}
</style>
